<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 内容卡片区 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-input
        placeholder="请输入内容"
        v-model="page.query"
        class="input-with-select"
        clearable
        @clear='clearselect'
      >
      <!-- 搜索按钮 -->
      <el-button slot="append" icon="el-icon-search" @click.enter="goodssearch"></el-button>
      </el-input>
      <!-- 添加商品按钮 -->
      <el-button type="primary" size="medium" @click="addgoods">添加商品</el-button>
      <!-- 商品列表表格 -->
      <el-table :data="goodslist.goods" style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160px"> 
            <template slot-scope="scope">
                {{scope.row.add_time|dateFormater}}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
              <!-- 编辑按钮 -->
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editgood(scope.row)"></el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deletegood(scope.row)"
          ></el-button>
          </template>
        
        </el-table-column>
      </el-table>

      <div class="block">
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pagenum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodslist.total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表
      goodslist: "",
      // 页面设置
      page: {
        //查询参数
        query: "",
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10,
      },
    };
  },
  created() {
    //   获取商品列表
    this.getgoodslist();
  },
  methods: {
    //   获取商品列表
    async getgoodslist() {
      const { data: res } = await this.$http.get("goods", {
        params: this.page,
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.goodslist = res.data;
      console.log(res);
    },
    // 搜索商品
   async goodssearch(){
     const { data: res } = await this.$http.get("goods", {
        params: this.page,
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.goodslist = res.data;
    },
    // 搜索框清空回调函数
    clearselect(){
     this.getgoodslist();
    },
    // 页面大小改变回调函数 val：页面大小
    handleSizeChange(val) {
      //  console.log(val);
      this.page.pagesize = val;
      this.getgoodslist();
    },
    // 当前页改变回调函数 val:当前页
    handleCurrentChange(val) {
      //   console.log(val);
      this.page.pagenum = val;
      this.getgoodslist();
    },
    //点击添加按钮回调函数
    addgoods(){
        console.log(this);
        // 路由跳转
        this.$router.push('/Addgoods')
    },
    // 点击删除按钮会点函数 ， 删除商品
    deletegood(good){
       this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const {data:res}=await this.$http.delete(`goods/${good.goods_id}`)
          if(res.meta.status===200) 
          {
            this.getgoodslist();
           return this.$message({
          message: res.meta.msg,
          type: 'success'
        });
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },
    // 编辑按钮点击事件回调函数
    editgood(good){
       this.$router.push({
        
         path:'/Editgood',
         query:{
           good:good
         }
       })
    }
  },
};
</script>

<style scoped>
.input-with-select {
  width: 30%;
  margin-right: 15px;
}
</style>